package com.example.template.ui.market.page;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.base.http.ApiObserver;
import com.example.base.http.HttpClient;
import com.example.base.ui.BaseFragment;
import com.example.base.ui.viewbinding.DataBindingConfig;
import com.example.base.utils.ToastUtils;
import com.example.common.constant.Constant;
import com.example.template.R;
import com.example.template.databinding.FragmentMarketListBinding;
import com.example.template.http.entity.CommunityEntity;
import com.example.template.http.entity.CommunityListData;
import com.example.template.http.service.CommunityApiService;
import com.example.template.ui.community.state.CommunitySquareViewModel;
import com.example.template.ui.market.adapter.MarketAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MarketListFragment extends BaseFragment implements OnRefreshLoadMoreListener {

    private CommunitySquareViewModel viewModel;
    private MarketAdapter adapter;
    private FragmentMarketListBinding binding;

    public static MarketListFragment newInstance() {
        return new MarketListFragment();
    }

    @Override
    protected void initViewModel() {
        viewModel = getFragmentScopeViewModel(CommunitySquareViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_market_list, BR.vm,viewModel);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = (FragmentMarketListBinding) getBinding();

        adapter = new MarketAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        binding.refreshLayout.recyclerView.setLayoutManager(linearLayoutManager);
        binding.refreshLayout.recyclerView.setAdapter(adapter);
        adapter.setEmptyView(R.layout.common_empty_view);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ToastUtils.toast("点击：" +position);
            }
        });
        binding.refreshLayout.smartRefreshLayout.setOnRefreshLoadMoreListener(this);
        binding.refreshLayout.smartRefreshLayout.autoRefresh();//第一次进入触发自动刷新
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//        getData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        viewModel.pageNum.set(1);
        getData();
    }

    private void getData() {
        HttpClient.getInstance().create(CommunityApiService.class)
                .getTrendsListByHot(viewModel.pageNum.get(), Constant.PAGE_SIZE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiObserver<CommunityListData<CommunityEntity>>() {
                    @Override
                    public void onResult(CommunityListData<CommunityEntity> listResponseData) {
                        if (viewModel.pageNum.get() == 1) {
                            viewModel.dataList.set(listResponseData.getResult().getItems());
                            binding.refreshLayout.smartRefreshLayout.finishRefresh();
                        } else {
                            for (CommunityEntity item: listResponseData.getResult().getItems()) {
                                viewModel.dataList.get().add(item);
                            }
                            binding.refreshLayout.smartRefreshLayout.finishLoadMore();
                        }
                        adapter.setDiffNewData(viewModel.dataList.get());
                        viewModel.hasNext.set(listResponseData.getResult().hasNext());
                        if (!viewModel.hasNext.get()) {
                            binding.refreshLayout.smartRefreshLayout.setNoMoreData(true);
                        }
                        viewModel.pageNum.set(viewModel.pageNum.get() + 1);
                    }
                });
    }
}
